﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WebNavigationApi.Models;
using WebNavigationApi.Models.ViewModel;

namespace WebNavigationApi.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class BookMarkController : ControllerBase
    {
        private WNContext _context;
        private IHttpContextAccessor _httpContextAccessor;
        public BookMarkController(WNContext context, IHttpContextAccessor httpContextAccessor)
        {
            _context = context;
            _httpContextAccessor = httpContextAccessor;
        }

        [Authorize]
        [Route("GetBookMarkType")]
        [HttpGet]
        public IActionResult GetBookMarkType()
        {

        

           var userID = User.Claims.Where(p => p.Type == "userID").FirstOrDefault().Value;
            var TypeList =  _context.WN_BOOKMARK_TYPE
                .Where(p => p.USER_ID == userID)
                .ToList()
                .Select(p=>new {id=p.ID,name=p.NAME});
            return Ok(new ResultList(true, 200, "", TypeList.ToArray(), TypeList.Count()));
        }

        [Authorize]
        [Route("GetBookMarkByType")]
        [HttpGet]
        public IActionResult GetBookMarkByType(string type,string name)
        {
            var headers = _httpContextAccessor.HttpContext.Request.Headers;
            var pageSize = headers["pageSize"].FirstOrDefault()?.ToString();
            var pageIndex = headers["pageIndex"].FirstOrDefault()?.ToString();
            var page = 1;
            var size = 5;

            if (!string.IsNullOrEmpty(pageSize))
            {
                size = Int32.Parse(pageSize);
            }
            if (!string.IsNullOrEmpty(pageIndex))
            {
                page = Int32.Parse(pageIndex);
            }

            var userID = User.Claims.Where(p => p.Type == "userID").FirstOrDefault().Value;
            var bookMarkList = _context.WN_BOOKMARK
                .Where(p => p.USER_ID == userID)
                .WhereIf(p => p.TYPE == type, !string.IsNullOrEmpty(type))
                .WhereIf(p => p.NAME.Contains(name), !string.IsNullOrEmpty(name))
                .ToList();
            var result = bookMarkList
                 .Skip(size * (page - 1)).Take(size)
                .Select(p => new {
                    id = p.ID,
                    icon = p.ICON,
                    name = p.NAME,
                    type =p.TYPE,
                    url = p.URL
                });
            return Ok(new ResultList(true, 200, "", result.ToArray(), bookMarkList.Count()));
        }


        [Authorize]
        [Route("SaveBookMarkType")]
        [HttpPost]
        public IActionResult SaveBookMarkType(TypeRequest request)
        {
            var userID = User.Claims.Where(p => p.Type == "userID").FirstOrDefault().Value;
            if (request.id == null)
            {
                var type = new WN_BOOKMARK_TYPE()
                {
                    ID = Guid.NewGuid().ToString("N"),
                    NAME = request.name,
                    USER_ID = userID
                };
                _context.Add(type);
            }
            else
            {
                var type = new WN_BOOKMARK_TYPE()
                {
                    ID = request.id,
                    NAME = request.name,
                    USER_ID = userID
                };
                _context.Update(type);
            }
            _context.SaveChanges();
            return Ok(new Result(true, 200, "添加或更新书签类型成功", ""));
        }


        [Authorize]
        [Route("DeleteBookMarkType")]
        [HttpGet]
        public IActionResult DeleteBookMarkType(string id)
        {

            var entity = _context.WN_BOOKMARK_TYPE.Where(p => p.ID == id).FirstOrDefault();
            _context.WN_BOOKMARK_TYPE.Remove(entity);
            _context.SaveChanges();
            return Ok(new Result(true, 200, "删除书签类型成功", ""));
        }




        [Authorize]
        [Route("SaveBookMark")]
        [HttpPost]
        public IActionResult SaveBookMark(BookMarkRequest request)
        {
            var userID = User.Claims.Where(p => p.Type == "userID").FirstOrDefault().Value;
            if (request.id == null)
            {
                var bookmark = new WN_BOOKMARK()
                {
                    ID = Guid.NewGuid().ToString("N"),
                    NAME = request.name,
                    ICON = request.icon,
                    TYPE = request.type,
                    URL = request.url,
                    REMARK = request.remark,
                    USER_ID = userID,
                    CREATETIME = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"),

                };
                _context.Add(bookmark);
            }
            else
            {
                var bookmark = new WN_BOOKMARK()
                {
                    ID = request.id,
                    NAME = request.name,
                    ICON = request.icon,
                    TYPE = request.type,
                    URL = request.url,
                    REMARK = request.remark,
                    USER_ID = userID,
                    UPDATETIME = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"),

                };
                _context.Update(bookmark);
            }
            _context.SaveChanges();
            return Ok(new Result(true, 200, "添加或更新书签成功", ""));
        }


        [Authorize]
        [Route("DeleteBookMark")]
        [HttpGet]
        public IActionResult DeleteBookMark(string id)
        {

            var entity = _context.WN_BOOKMARK.Where(p => p.ID == id).FirstOrDefault();
            _context.WN_BOOKMARK.Remove(entity);
            _context.SaveChanges();
            return Ok(new Result(true, 200, "删除书签成功", ""));
        }


        [Authorize]
        [Route("GetBookMarkItem")]
        [HttpGet]
        public IActionResult GetBookMarkItem(string id)
        {

            var result = _context.WN_BOOKMARK.Where(p => p.ID == id)
                .Select(p => new { 
                    id = p.ID,
                    icon = p.ICON,
                    name = p.NAME,
                    type = p.TYPE,
                    url = p.URL,
                    remark = p.REMARK
                });
            
            return Ok(new Result(true, 200, "删除书签成功", result));
        }
    }
}
